package com.tan.api.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * 自定义反序列化器用于将字符串转换为 LocalDateTime
 * @author tan
 */
public class LocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public LocalDateTimeDeserializer() {
        // 调用父类构造函数
        this(null);
    }

    protected LocalDateTimeDeserializer(Class<?> vc) {
        // 指定反序列化的类型
        super(vc);
    }

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        // 从 JSON 输入流中读取字符串，并使用指定格式解析为 LocalDateTime
        return LocalDateTime.parse(p.getValueAsString(), FORMATTER);
    }
}
